package com.ultra.template.parse;

import com.ultra.enums.ArgumentTypeEnum;
import com.ultra.exception.UnableResolveTypeException;
import com.ultra.exception.UnknownTypeException;
import com.ultra.template.content.Argument;
import com.ultra.template.content.Pairs;
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;


/**
 * @author yinger
 * @description TODO
 * @date 2024/12/30
 **/
public final class GenericCollectionParser {
    public static void _fromCollection(Argument config) throws UnableResolveTypeException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, UnknownTypeException {
        config.setIThCollection(Parser.times[config.getDepth()]++);
        config.setArgumentType(ArgumentTypeEnum.COLLECTION);
        // 获取集合的泛型信息
        Type type = config.getType();
        config.setClassName(((ParameterizedTypeImpl) type).getRawType().getName());
        // 递进前先设置Type
        if (config.getChild() == null || config.getChild().length == 0) {
            config.setChild(new Argument[]{new Argument()});
        }
        config.getChild()[0].setType(((ParameterizedTypeImpl) type).getActualTypeArguments()[0]);
        config.getChild()[0].setDepth(config.getDepth() + 1);
        config.getChild()[0].setIThArgument(config.getIThArgument());
        Parser.flatteningMap.put(new Pairs(config.getIThArgument(),config.getDepth(),config.getIThCollection()),config);
        Parser.parserDispatcher(config.getChild()[0]);
    }
}